<?php
class ImageCategoriesController extends AppController {
	public $name = 'ImageCategories';
	public $uses=array('ImageCategory','Image','FriendlyUrl','FileManager');
	
	function admin_index(){
		//get filter field
		$filterField=array(				
			$this->uses[0].'.title'=>_resource('title',true),
		);
		
		$this->set('filterField',$filterField);
		//set list cac country name ra view
		if($this->webConfig['is_multi_lang'])
			$this->set('listLang',$this->Common->listCountry());
		//===================================================================
		$this->render('admin_index','ajax');
	}	
	
	function admin_lists(){
		//khoi tao cac session can thiet cho 1 model, session nay dung de luu lai trang thai cua 1 model nhu phan trang, sort tang giam, dieu kien search, id cha...
		//cac su dung Session cua cakephp : doc them trong book.cakephp.org
		if(!$this->Session->check($this->uses[0])){
			//khoi tao array vao bien $tmp
			$tmp=array(
				'limit'=>$this->webConfig['default_row_per_page'],
				'conditions'=>'',
				'fields'=>null,
				'order'=>$this->uses[0].'.id desc',
				'page'=>1,
				'recursive'=>0,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>'',
				'filterText'=>'',
				'catid'=>'',
			);
			//luu vao session
			$this->Session->write($this->uses[0],$tmp);			
		}
		
		
		$this->{$this->uses[0]}->locale=$this->Session->read('adminLang');
		//doc ra cac trang thai cua 1 model da luu trong session
		// truyen vao bien paginate, khi lay data, model se dua vao cac gia tri cua bien nay de search, sort, phan trang ... 
		$this->paginate=$this->Session->read($this->uses[0]);				
		//ham paginate() la ham ho tro phan trang, khi dung ham nay, se tu dong phan trang theo cac gia tri trong bien paginate o tren
		//lay data da duoc phan trang va truyen vao bien $data0
		$data0=$this->paginate($this->uses[0]);
		//t($data0);
		//su dung khi site co chuc nang da ngon ngu==========================
		//duyet qua tat ca cac rows trong $data0 de kiem tra xem data cua rows da co ngon ngu nao roi.
		if($this->webConfig['is_multi_lang']){
			foreach($data0 as $k=>$v){
				$data0[$k]['Langs']=$this->getLangs($v[$this->uses[0]]['id'],$this->uses[0]);
			}
			$this->set('listLang',$this->Common->listCountry());
		}
		//t($data0);
		//===================================================================
		//set bien $data0 ra view de hien ra luoi.
		$this->set('data0',$data0);
		
		
		//render view va cho cakephp biet day la view cua ajax -> khi do, cakephp se chi render view ma khong render toan bo layout cua website. 
		$this->render('admin_lists','ajax');
	}
	
	function admin_add($locale=null,$id=null){
		//su dung khi site co chuc nang da ngon ngu==========================
		//neu khong truyen vao ngon ngu thi ngon ngu mac dinh cua data duoc them vao se la ngon ngu duoc doc tu session
		if(!$locale){
			$locale=$this->Session->read('adminLang');
		}
		//khai bao ngon ngu cua model, khi do, ngon ngu cua data duoc luu vao database la $locale 
		$this->{$this->uses[0]}->locale=$locale;
		$this->{$this->uses[1]}->locale=$locale;
		//====================================================================		
		$this->{$this->uses[0]}->recursive=0;				
		$id=intval($id);
		$isInsert=false;
		if(!$id){
			//add new album
			$title='Untitle Album';
			$this->{$this->uses[0]}->save(array($this->uses[0]=>array('title'=>$title,'published'=>0)));
			$id=	$this->{$this->uses[0]}->getLastInsertId();
			//================seo link add
			//$this->updateSeoLink('/lifes/index/'.$id.'/'.$locale, $title, $id,$this->uses[0]);
			
			$this->Common->updateCacheRoute(false);
			//=======================
			
			$this->admin_edit($id,$locale)	;
		}
		else{
			if(!empty($this->data)){
				$data=$this->{$this->uses[0]}->read(null,$id);
				if(!empty($data)){
					//Cat Image
					//default image
					
					if(empty($data[$this->uses[0]]['image']) && count($this->data[$this->uses[1]]['image'])>0){
						$data[$this->uses[0]]['image']=array_values($this->data[$this->uses[1]]['image']);
						$data[$this->uses[0]]['image']=$data[$this->uses[0]]['image'][0];
					}
					$data[$this->uses[0]]['title']=$this->data[$this->uses[0]]['title'];
					$data[$this->uses[0]]['published']=1;
					$this->{$this->uses[0]}->save($data);					
					
					//=================seo link update
					$this->updateSeoLink('/lifes/index/'.$this->data[$this->uses[0]]['id'].'/'.$locale, $this->data[$this->uses[0]]['title'], $this->data[$this->uses[0]]['id']);
					
					//update cache
					//=======================================
					//t($data);
					//Image
					$this->{$this->uses[1]}->recursive=-1;
					if(isset($this->data[$this->uses[1]]['title']) && count($this->data[$this->uses[1]]['title'])>0){
						$i=0;
						foreach($this->data[$this->uses[1]]['title'] as $k=>$v){
							$img=$this->{$this->uses[1]}->find('first',array('conditions'=>array($this->uses[1].'.image'=>$k)));
							$img[$this->uses[1]]['title']=$v;
							$img[$this->uses[1]]['view_order']=$i++;
							$img[$this->uses[1]]['tag']=$this->data[$this->uses[1]]['tag'][$k];
							if(empty($v))$v='Untitle image';
							$this->{$this->uses[1]}->save($img);
							//=================seo link update
							$this->updateSeoLink('/lifes/detail/'.$img[$this->uses[1]]['id'].'/'.$v, $v, $img[$this->uses[1]]['id']);
							
							//=======================================
							
						}
					}
					$this->Common->updateCacheRoute(false);
					$isInsert=true;
				}
				else{
					$this->Session->write('error',_resource('can not found data',true));
				}
			}
		}
		
		//======================
		//set bien locale ra view de su dung
		
	}

	function admin_edit($id=null,$locale=null){	
		if(!$locale){
			$locale=$this->Session->read('adminLang');
		}
		$id=intval($id);
		$this->{$this->uses[0]}->locale=$locale;
		$this->{$this->uses[1]}->locale=$locale;		
		$name_error=_resource('can not be null',true);		
		$isInsert=false;
		$this->{$this->uses[0]}->recursive=0;
		if(!empty($this->data)){
			$data=$this->{$this->uses[0]}->read(null,$id);
			if(!empty($data)){
				//Cat Image
				//default image
				
				if(empty($data[$this->uses[0]]['image']) && count($this->data[$this->uses[1]]['image'])>0){
					$data[$this->uses[0]]['image']=array_values($this->data[$this->uses[1]]['image']);
					$data[$this->uses[0]]['image']=$data[$this->uses[0]]['image'][0];
				}
				$data[$this->uses[0]]['title']=$this->data[$this->uses[0]]['title'];				
				$this->{$this->uses[0]}->save($data);

				//=================seo link update
				//$this->updateSeoLink('/lifes/index/'.$this->data[$this->uses[0]]['id'].'/'.$locale, $this->data[$this->uses[0]]['title'], $this->data[$this->uses[0]]['id'],$this->uses[0]);
				
				//update cache
				//=======================================
				//t($data);
				//Image
				$this->{$this->uses[1]}->recursive=-1;
				
				if(isset($this->data[$this->uses[1]]['title']) && count($this->data[$this->uses[1]]['title'])>0){
					$i=0;
					foreach($this->data[$this->uses[1]]['title'] as $k=>$v){
						$img=$this->{$this->uses[1]}->find('first',array('conditions'=>array($this->uses[1].'.image'=>$k)));
						$img[$this->uses[1]]['title']=$v;
						$img[$this->uses[1]]['view_order']=$i++;
						$img[$this->uses[1]]['tag']=$this->data[$this->uses[1]]['tag'][$k];
						if(empty($v))$v='Untitle image';
						$this->{$this->uses[1]}->save($img);
						
						//=================seo link update
						//$this->updateSeoLink('/lifes/detail/'.$img[$this->uses[1]]['id'].'/'.$locale, $v, $img[$this->uses[1]]['id']);
						
						
						//=======================================
						
					}
				}
				$this->Common->updateCacheRoute(false);
				$isInsert=true;
			}
			else{
				$this->Session->write('error',_resource('can not found data',true));
			}
		}else{
			if($id!=null){			
					
				$data =$this->{$this->uses[0]}->read(null,$id);				
				$this->data=$data;
				$data =$this->{$this->uses[1]}->find('all',array('conditions'=>array('image_category_id'=>$id),'order'=>$this->uses[1].'.view_order'));
				$this->set('imgdata',$data);
				if(empty($this->data)){	
					$this->Session->write('error',_resource('data not found',true));				
					$this->render('admin_editNotFound');
				}
			}
			else{				
				$this->Session->write('error',_resource('data not found',true));		
				$this->render('admin_edit_not_found');
			}
		}	

		
		
		//set bien locale ra view de su dung
		$this->set('locale',$locale);
		if($this->webConfig['is_multi_lang'])
			$this->set('listLang',$this->Common->listCountry());
		$this->set('id',$id);
		$this->set('isInsert',$isInsert);
		$this->set('name_error',$name_error);
		$this->render('admin_edit','ajax');
		
	}

	function admin_delete($id = null) {		
		$this->Session->delete('error');
		//if is submit:		
		if($this->request->is('post')){
			$id_arr=$this->data['check'];
		} else {
			$id_arr=null;
		}
		//if url
		if(intval($id)){
			$id_arr[$id]='';
		}
		if(count($id_arr)>0){
			//Xoa nhieu record
			foreach($id_arr as $k=>$v)$this->{$this->uses[0]}->delete($k);				
		} else { 
			$this->Session->write('error',_resource('no information to delete',true));
		}
		$this->admin_lists();		
	}
	
	function admin_published($id=null,$value=1,$field=0){
	$id_arr=null;
		$all=false;		
		switch($field){
			case 1: $fieldname='best';break;
			case 2: $fieldname='bestbuy';break;
			default: $fieldname='published';
		}
		if((!intval($value) && $value!=0) || intval($value)>1){
			$value=1;
		}
		
		//if is submit:		
		if($this->request->is('post')){
			$id_arr=$this->data['check'];
			$all=true;
		} 
		
		//if url
		if(intval($id) && !$all){
			$id_arr[$id]='';
		}
		
		if(count($id_arr)>0){			
			$this->{$this->uses[0]}->query('update '.$this->{$this->uses[0]}->tablePrefix.$this->{$this->uses[0]}->table.' set '.$fieldname.'='.$value.' where id in ('.implode(',',array_keys($id_arr)).')' );
		}
		if (!$all) {			
			$this->set('value',$value);
			$this->set('field',$field);
			$this->set('fieldname',$fieldname);
			$this->set('id',intval($id));
			$this->render('admin_published','ajax');		
		} else {
			$this->admin_lists();
		}
		
	}
	
	function admin_sort($sortField=null,$sortDir='asc'){
		if($sortField){
			if($sortDir!='desc') $sortDir='asc';
			$this->Session->write($this->uses[0].'.sortField',$sortField);
			$this->Session->write($this->uses[0].'.sortDir',$sortDir);
			$this->Session->write($this->uses[0].'.order',$sortField.' '.$sortDir);
		}
		$this->admin_lists();
	}
	
	function admin_page($page=1){
		$this->Session->write($this->uses[0].'.page',$page);
		$this->admin_lists();
	}
	
	function admin_display($limit=0){
		if(intval($limit)){
			$this->Session->write($this->uses[0].'.limit',$limit);
		}
		$this->admin_lists();
	}

	function admin_filter(){
		if($this->request->is('post')){
			$this->Session->write($this->uses[0].'.filterText',$this->request->data['filter_text']);
			$this->Session->write($this->uses[0].'.filterField',$this->request->data['filter_field']);
			//$conditions=$this->Session->read($this->uses[0].'.conditions');
			if(!empty($this->request->data['filter_text'])){				
				$conditions[$this->request->data['filter_field'].' like']='%'.$this->request->data['filter_text'].'%';
				$this->Session->write($this->uses[0].'.page',1);				
			}
			else{
				unset($conditions[$this->request->data['filter_field'].' like']);
			}
			
			$this->Session->write($this->uses[0].'.conditions',$conditions);
		}
		$this->admin_lists();
	}
	
function admin_albumcover($filename,$id){
		$this->autoRender=false;
		$id=intval($id);
		if($filename && $id){
			$olddata=$this->{$this->uses[0]}->findById($id);
			if($olddata[$this->uses[0]]['image']!=$filename){
				$this->{$this->uses[0]}->updateAll(array($this->uses[0].'.image'=>'\''.$filename.'\''),array($this->uses[0].'.id'=>$id));
				//bound
				$this->FileManager->updateAll(array('mount'=>'mount + 1'),array('name'=>$filename));
				//unbound
				$this->FileManager->updateAll(array('mount'=>'mount - 1'),array('name'=>$olddata[$this->uses[0]]['image']));
				echo "<script>alert('done');</script>";
			}
		}
	}
	
	/*
	 * ham dung de save thu tu cua tung mau tin
	 */
	function admin_save(){
		if(isset($this->params['form']['order'])){			
			foreach($this->params['form']['order'] as $k=>$v){
				if(intval($v)){
					//set view order for category
					$this->{$this->uses[0]}->updateAll(array($this->uses[0].'.view_order'=>intval($v)),array($this->uses[0].'.id'=>$k));
				}
			}
		}
		$this->Session->write('message',_resource('view order has been update',true));
		$this->admin_lists();
	}
	
/*
	 * ham dung khi co su dung chuc nang da ngon ngu
	 * dung de change locale cua he thong
	 * tham so truyen vao la locale cua ngon ngu
	 */
	function admin_changeLocale($locale=null){
		//luu session
		if($locale){
			$this->Session->write('adminLang',$locale);
		}
		//reload lai luoi de apply locale vua change
		$this->admin_lists();
	}

	function index($page=1){
		$this->layout='default';
		$this->set('css','catimg');
		
		//if(!$this->Session->check($this->uses[0])){
			//khoi tao array vao bien $tmp
			$tmp=array(
				'limit'=>$this->webConfig['catimage_per_page'],
				'conditions'=>'',
				'fields'=>null,
				'order'=>$this->uses[0].'.id desc',
				'page'=>intval($page)>0?$page:1,
				'recursive'=>0,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>'',
				'filterText'=>'',
				'catid'=>'',
			);
			//luu vao session
			//$this->Session->write($this->uses[0],$tmp);			
		//}
		//if(intval($page)>0)$this->Session->write($this->uses[0].'.page',$page);
		
		$this->{$this->uses[0]}->locale=$this->Session->read('siteLang');
		//doc ra cac trang thai cua 1 model da luu trong session
		// truyen vao bien paginate, khi lay data, model se dua vao cac gia tri cua bien nay de search, sort, phan trang ... 
		$this->paginate=$tmp;				
		//ham paginate() la ham ho tro phan trang, khi dung ham nay, se tu dong phan trang theo cac gia tri trong bien paginate o tren
		//lay data da duoc phan trang va truyen vao bien $data0
		$data0=$this->paginate($this->uses[0]);
		$this->set('title_for_layout','Hình ảnh SCY');
		//t($data0);		
		//set bien $data0 ra view de hien ra luoi.
		$this->set('data0',$data0);
		
		
	}

}
?>
